#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include "gy39.h"

#define LED7	"/sys/kernel/gec_ctrl/led_d7"
#define LED8	"/sys/kernel/gec_ctrl/led_d8"
#define LED9	"/sys/kernel/gec_ctrl/led_d9"
#define LED10	"/sys/kernel/gec_ctrl/led_d10"
#define LEDALL	"/sys/kernel/gec_ctrl/led_all"
#define BEEP	"/sys/kernel/gec_ctrl/beep"
#define ON	1
#define	OFF	0
#define YUZHI 700

int led_on = 0;
extern int x_read;
extern int y_read;
extern int data[5];


void led_ctl(char *led_id, int onoff)
{
	int fd = open(led_id, O_RDWR);
	if(-1 == fd)
	{
		perror("open led failed");
		return ;
	}
	
	if(ON == onoff)
	{
		write(fd, &onoff, 4);
	}
	else
	{
		write(fd, &onoff, 4);
	}
	
	close(fd);
	return ;
}

void led()
{
	int x = -1,y = -1;
	lcd_draw_rectangel(0, 0, 800, 480, 0xffffff);
	if(led_on == 0)
	{
		lcd_draw_rectangel(200,200,200,200,0x000000);
	}
	else
	{
		lcd_draw_rectangel(200,200,200,200,0xffff00);
	}
	lcd_draw_rectangel(700, 0, 100, 100, 0xffff);
	
	while(1)
	{
		if(x!=x_read||y!=y_read && x_read!=-1&&y_read!=-1)
		{
			x = x_read;
			y = y_read;
			x_read = -1;
			y_read = -1;
		}
		
		if(x>=200&&x<400&&y>=200&&y<400)
		{
			if(led_on == 0)//点灯
			{
				led_ctl(LEDALL, 1);
				led_on = 1;
				lcd_draw_rectangel(200,200,200,200,0xffff00);
				sleep(1);
			}
			else if(led_on == 1)//熄灯
			{
				led_ctl(LEDALL, 0);
				led_on = 0;
				lcd_draw_rectangel(200,200,200,200,0xfffffff);
				sleep(1);
			}
		}
		else if(x>=700&&x<800 && y<100&&y>=0)
		{
			break;
		}
		
		if(data[0]<=YUZHI)//点灯
		{
			led_ctl(LEDALL, 1);
			led_on = 1;
			lcd_draw_rectangel(200,200,200,200,0xffff00);
		}
		else if(data[0]>YUZHI)//熄灯
		{
			led_ctl(LEDALL, 0);
			led_on = 0;
			lcd_draw_rectangel(200,200,200,200,0xfffffff);
		}
		x = -1;
		y = -1;
	}
	
}